博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
react dnd demo2
阅读量:4360 次
发布时间:2019-06-07

本文共 4909 字,大约阅读时间需要 16 分钟。

import React, { Component } from 'react';import './App.css';import Card from './Card';import HTML5Backend from 'react-dnd-html5-backend'import { DragDropContext } from 'react-dnd'// const update = require('immutability-helper');import update from 'react-addons-update';class App extends Component {  state = {    cards: [      {        id: 1,        text: 'Write a cool JS library',      },      {        id: 2,        text: 'Make it generic enough',      },      {        id: 3,        text: 'Write README',      },      {        id: 4,        text: 'Create some examples',      },      {        id: 5,        text:          'Spam in Twitter and IRC to promote it (note that this element is taller than the others)',      },      {        id: 6,        text: '???',      },      {        id: 7,        text: 'PROFIT',      },    ],  }  deleteItem = id => {    this.setState(prevState => {      return {        items: prevState.items.filter(item => item.id !== id)      }    })  }  moveCard = (dragIndex, hoverIndex) => {    const { cards } = this.state    const dragCard = cards[dragIndex]    this.setState(      update(this.state, {        cards: {          $splice: [[dragIndex, 1], [hoverIndex, 0, dragCard]],        },      }),     () => {      console.log(this.state.cards);    })  }  render() {    return (      
{
/*
logo */}

Welcome to React

{
/* {this.state.items.map((item, index) => (
this.deleteItem(id)} /> ))} */}
{
/*
*/}
{
this.state.cards.map((card, i) => (
))}
); }}export default DragDropContext(HTML5Backend)(App);

card

import React from 'react';import PropTypes from 'prop-types';import { findDOMNode } from 'react-dom';import {  DragSource,  DropTarget,  ConnectDropTarget,  ConnectDragSource,  DropTargetMonitor,  DropTargetConnector,  DragSourceConnector,  DragSourceMonitor,} from 'react-dnd';import { XYCoord } from 'dnd-core';import flow from 'lodash/flow';const style = {  border: '1px dashed gray',  padding: '0.5rem 1rem',  marginBottom: '.5rem',  backgroundColor: 'white',  cursor: 'move',};const cardSource = {  beginDrag(props) {    return {      id: props.id,      index: props.index,    }  },};const cardTarget = {  hover(props, monitor, component) {    const dragIndex = monitor.getItem().index    const hoverIndex = props.index    // Don't replace items with themselves    if (dragIndex === hoverIndex) {      return;    }    // Determine rectangle on screen    const hoverBoundingRect = (findDOMNode(      component,    )).getBoundingClientRect();    // Get vertical middle    const hoverMiddleY = (hoverBoundingRect.bottom - hoverBoundingRect.top) / 2;    // Determine mouse position    const clientOffset = monitor.getClientOffset();    // Get pixels to the top    const hoverClientY = (clientOffset).y - hoverBoundingRect.top;    // Only perform the move when the mouse has crossed half of the items height    // When dragging downwards, only move when the cursor is below 50%    // When dragging upwards, only move when the cursor is above 50%    // Dragging downwards    if (dragIndex < hoverIndex && hoverClientY < hoverMiddleY) {      return;    }    // Dragging upwards    if (dragIndex > hoverIndex && hoverClientY > hoverMiddleY) {      return;    }    // Time to actually perform the action    props.moveCard(dragIndex, hoverIndex);    // Note: we're mutating the monitor item here!    // Generally it's better to avoid mutations,    // but it's good here for the sake of performance    // to avoid expensive index searches.    monitor.getItem().index = hoverIndex;  },}class Card extends React.Component {  static propTypes = {    connectDragSource: PropTypes.func.isRequired,    connectDropTarget: PropTypes.func.isRequired,    index: PropTypes.number.isRequired,    isDragging: PropTypes.bool.isRequired,    id: PropTypes.any.isRequired,    text: PropTypes.string.isRequired,    moveCard: PropTypes.func.isRequired,  }  render() {    const {      text,      isDragging,      connectDragSource,      connectDropTarget,    } = this.props;    const opacity = isDragging ? 0 : 1;    return (      connectDragSource &&      connectDropTarget &&      connectDragSource(        connectDropTarget(
{text}
), ) ); }}export default flow( DragSource( 'card', cardSource, (connect, monitor) => ({ connectDragSource: connect.dragSource(), isDragging: monitor.isDragging(), }), ), DropTarget('card', cardTarget, (connect) => ({ connectDropTarget: connect.dropTarget(), })))(Card);

 

转载于:https://www.cnblogs.com/l8l8/p/10791798.html

你可能感兴趣的文章
Intel Core Microarchitecture Pipeline
查看>>
如何去除交叉表的子行(列)的小计?
查看>>
Web字体(链接)嵌入
查看>>
switch… case 语句的用法
查看>>
day07补充-数据类型总结及拷贝
查看>>
语言、数据和运算符
查看>>
正则表达式30分钟入门教程
查看>>
sqlserver try catch·
查看>>
怎么在三维世界里叙述五维故事
查看>>
1028: 可乐(2018年中南大学研究生复试机试题 )
查看>>
珍藏的最全的windows操作系统快捷键
查看>>
【DBAplus】SQL优化:一篇文章说清楚Oracle Hint的正确使用姿势
查看>>
二叉树结点删除操作
查看>>
图论-单源最短路-SPFA算法
查看>>
转换文件的字符集
查看>>
软件质量理解
查看>>
jquery 在 table 中修改某行值
查看>>
pyc文件是什么【转载】
查看>>
POM.xml 标签详解
查看>>
hdu 3635 Dragon Balls (并查集)
查看>>